home *** CD-ROM | disk | FTP | other *** search
- //***************************************************************************
- // OATH :: Object-oriented Abstract Type Hierarchy
- //***************************************************************************
- //
- // Copyright (C) 1991, 1990 Texas Instruments Incorporated
- // Permission is granted to any individual or institution
- // to use, copy, modify, and distribute this software,
- // provided that this complete copyright and permission notice
- // is maintained, intact, in all copies and supporting documentation.
- //
- // Texas Instruments Incorporated provides this software "as is"
- // without express or implied warranty.
- //
- //***************************************************************************
- // characterSet (characterSetA, characterSetG)
- //
- // History:
- // 07/91 Brian M Kennedy import, export, typeRegister
- // 06/91 Brian M Kennedy New macros & format; remove printDiagnostic
- // 04/91 Brian M Kennedy Derive from finiteSet (was a set)
- // 12/90 Brian M Kennedy Original
- //
- //***************************************************************************
-
- #include "copyright.h"
-
- #include <oath/characterSet.h>
-
- #include <iostream.h>
-
- /////////////////////////////////////////////////////////////////////////////
- // characterSet Outlines
-
- OUTLINES(characterSet, finiteSet)
-
- unsigned char characterSetG::BitMasks [8] = {1, 2, 4, 8, 16, 32, 64, 128};
-
- // characterSet Accessors //////////
-
- void characterSetG::
- setbits(unsigned char S, unsigned char B)
- {for(unsigned char C = S; C <= B; ++C)
- setbit(C);
- }
-
- void characterSetG::
- flipbits()
- {for(int I = 0; I < 32; ++I)
- Bytes[I] ^= 255;
- }
-
- // Constructors //////////
-
- characterSetG::
- characterSetG (int IsConst) // IsConst = FALSE);
- :finiteSetG(IsConst)
- {for(int I = 0; I < 32; ++I)
- Bytes[I] = 0;
- }
-
- characterSetG::
- characterSetG (const unsigned char iBytes[32], int IsConst) // = FALSE
- :finiteSetG(IsConst)
- {for(int I = 0; I < 32; ++I)
- Bytes[I] = iBytes[I];
- }
-
- characterSetG::
- characterSetG (const stringG* S, int IsConst) // IsConst = FALSE);
- :finiteSetG(IsConst)
- {for(int I = 0; I < 32; ++I)
- Bytes[I] = 0;
- if(!S->isEmpty())
- {int Invert = FALSE;
- stringPosA P = (stringPosA&)(S->makePos(0, 0));
- if((*P).value() == '^')
- {Invert = TRUE;
- ++P;
- }
- if(P())
- {unsigned char LastValue;
- while(1)
- {setbit(LastValue = (*P).value());
- ++P;
- if(!P())
- break;
- if((*P).value() == '-')
- {++P;
- if(!P())
- {setbit('-');
- break;
- }
- setbits(LastValue, (*P).value());
- ++P;
- if(!P())
- break;
- }
- }
- }
- if(Invert)
- flipbits();
- }
- }
-
-
- // oathCore Operations //////////
-
- void characterSetG::
- export (exportP& X) const
- {X.writeType(TypeName);
- X.stream().put(char(isConst()));
- for(int I = 0; I < 32; ++I)
- X.stream().put(Bytes[I]);
- }
-
- objA characterSetG::
- import (importP& M)
- {char MakeConst = M.stream().get();
- unsigned char Bytes[32]; // speedup by writing a constructor
- for(int I = 0; I < 32; ++I) // to do this directly
- Bytes[I] = M.stream().get();
- return new characterSetG (Bytes, MakeConst);
- }
-
-
- // obj Operations //////////
-
- int characterSetG::
- isEqual (const objG*) const
- {ensure(FALSE, "Sorry, not implemented!");
- return FALSE;
- }
-
-
- // bag Operations //////////
-
- int characterSetG::
- isEmpty () const
- {for(int I = 0; I < 32; ++I)
- if(Bytes[I]) return TRUE;
- return FALSE;
- }
-
- int characterSetG::
- count () const
- {int Count = 0;
- for(int I = 0; I < 32; ++I)
- {switch(Bytes[I])
- {case 1: case 2: case 4: case 8: case 16:
- Count += 1; break;
- case 3: case 5: case 6: case 9: case 10:
- case 12: case 17: case 18: case 20: case 24:
- Count += 2; break;
- case 7: case 11: case 13: case 14: case 19:
- case 21: case 22: case 25: case 26: case 28:
- Count += 3; break;
- case 15: case 23: case 27: case 29: case 30:
- Count += 4; break;
- case 31:
- Count += 5; break;
- }
- }
- return Count;
- }
-
- void characterSetG::
- apply (void (*)(objA)) const
- {ensure(FALSE, "Sorry, not implemented!");}
-
- bagG* characterSetG::
- applyX (objA (*)(objA), bagG* B) const
- {ensure(FALSE, "Sorry, not implemented!");
- return B;
- }
-
- bagG* characterSetG::
- applyX (bagA (*)(objA), bagG* B) const
- {ensure(FALSE, "Sorry, not implemented!");
- return B;
- }
-
-
- // set Operations //////////
-
- void characterSetG::
- subtract (const setG* S)
- {for(unsigned char C = 0; C <= 255; ++C)
- {if(getbit(C))
- if(S->contains(characterA::make(C).guts()))
- clearbit(C);
- }
- }
-
- void characterSetG::
- intersect (const setG* S)
- {for(unsigned char C = 0; C <= 255; ++C)
- {if(getbit(C))
- if(!S->contains(characterA::make(C).guts()))
- clearbit(C);
- }
- }
-
-
- // finiteSet Operations //////////
-
- void characterSetG::
- invert ()
- {flipbits();}
-
-
- //***************************************************************************
-